package com.dyz.leetcode;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

public class FindKthLargest215 {
    public int findKthLargest(int[] nums, int k) {
        //堆排序
        Queue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        }); //max heap
        for (int num : nums) {
            queue.offer(num);
        }
        int res = 0;
        while (queue.size() > 0) {
            int t = queue.poll();
            k--;
            if (k == 0) {
                res = t;
                break;
            }
        }
        return res;
    }

    public int findKthLargest1(int[] nums, int k) {
        //第二种解法
        Arrays.sort(nums);
        return nums[nums.length-k];
    }
}
